package com.originskyseed.platform.course.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.course.mapper.PaperMapper;
import com.originskyseed.platform.course.mapper.PaperRelateSubjectMapper;
import com.originskyseed.platform.course.mapper.PaperRelateSubjectOptionMapper;
import com.originskyseed.platform.course.model.Paper;
import com.originskyseed.platform.course.model.PaperRelateSubject;
import com.originskyseed.platform.course.model.PaperRelateSubjectOption;
import com.originskyseed.platform.course.model.TaskSelectPaperVO;
import com.originskyseed.platform.course.service.IPaperService;

/**
 * 试卷信息Service接口实现类（OSI_PAPER）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("paperService")
public class PaperServiceImpl implements IPaperService {

	@Resource
	private PaperMapper paperMapper;

	@Resource
	private PaperRelateSubjectMapper paperRelateSubjectMapper;

	@Resource
	private PaperRelateSubjectOptionMapper paperRelateSubjectOptionMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.paperMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.paperMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(Paper record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.paperMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Paper record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.paperMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(Paper record) throws Exception {
		record.setUpdateTime(new Date());
		return this.paperMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Paper record) throws Exception {
		record.setUpdateTime(new Date());
		return this.paperMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Paper selectByPrimaryKey(String id) throws Exception {
		return this.paperMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Paper> selectList(Paper record) throws Exception {
		return this.paperMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Paper> selectListByPage(Map<String, Object> params, BasePage<Paper> page) throws Exception {
		params.put("page", page);
		List<Paper> list = this.paperMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 插入记录 同时插入考卷试题
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertPaperAndPaperSubject(Paper record, List<PaperRelateSubject> paperRelateSubjectList) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());

		BigDecimal totalScore = BigDecimal.ZERO;
		// 新增考题
		if (paperRelateSubjectList != null && paperRelateSubjectList.size() > 0) {
			for (PaperRelateSubject paperRelateSubject : paperRelateSubjectList) {
				totalScore = totalScore.add(paperRelateSubject.getScore());
				paperRelateSubject.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				paperRelateSubject.setPaperId(record.getId());
				paperRelateSubject.setCreator(record.getCreator());
				paperRelateSubject.setCreatorId(record.getCreatorId());
				paperRelateSubject.setCreateTime(new Date());
				this.paperRelateSubjectMapper.insertSelective(paperRelateSubject);
				if (StringUtils.isNotEmpty(paperRelateSubject.getPaperRelSubOptListStr())) {
					List<PaperRelateSubjectOption> paperRelateSubjectOptionList = JSONArray.parseArray(paperRelateSubject.getPaperRelSubOptListStr(), PaperRelateSubjectOption.class);
					for (PaperRelateSubjectOption paperRelateSubjectOption : paperRelateSubjectOptionList) {
						paperRelateSubjectOption.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						paperRelateSubjectOption.setPaperId(record.getId());
						paperRelateSubjectOption.setStemId(paperRelateSubject.getId());
						paperRelateSubjectOption.setCreator(record.getCreator());
						paperRelateSubjectOption.setCreatorId(record.getCreatorId());
						paperRelateSubjectOption.setCreateTime(new Date());
						this.paperRelateSubjectOptionMapper.insertSelective(paperRelateSubjectOption);
					}
				}
			}
		}
		record.setTotalScore(totalScore);
		return this.paperMapper.insertSelective(record);
	}

	/**
	 * 更新记录 同时修改考卷试题
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updatePaperAndPaperSubject(Paper record, List<PaperRelateSubject> paperRelateSubjectList) throws Exception {
		record.setUpdateTime(new Date());

		BigDecimal totalScore = BigDecimal.ZERO;
		// 新增考题
		if (paperRelateSubjectList != null && paperRelateSubjectList.size() > 0) {
			this.paperRelateSubjectMapper.deleteByPaperId(record.getId());
			this.paperRelateSubjectOptionMapper.deleteByPaperId(record.getId());
			for (PaperRelateSubject paperRelateSubject : paperRelateSubjectList) {
				totalScore = totalScore.add(paperRelateSubject.getScore());
				paperRelateSubject.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				paperRelateSubject.setPaperId(record.getId());
				paperRelateSubject.setCreator(record.getCreator());
				paperRelateSubject.setCreatorId(record.getCreatorId());
				paperRelateSubject.setCreateTime(new Date());
				this.paperRelateSubjectMapper.insertSelective(paperRelateSubject);
				if (StringUtils.isNotEmpty(paperRelateSubject.getPaperRelSubOptListStr())) {
					List<PaperRelateSubjectOption> paperRelateSubjectOptionList = JSONArray.parseArray(paperRelateSubject.getPaperRelSubOptListStr(), PaperRelateSubjectOption.class);
					for (PaperRelateSubjectOption paperRelateSubjectOption : paperRelateSubjectOptionList) {
						paperRelateSubjectOption.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
						paperRelateSubjectOption.setPaperId(record.getId());
						paperRelateSubjectOption.setStemId(paperRelateSubject.getId());
						paperRelateSubjectOption.setCreator(record.getCreator());
						paperRelateSubjectOption.setCreatorId(record.getCreatorId());
						paperRelateSubjectOption.setCreateTime(new Date());
						this.paperRelateSubjectOptionMapper.insertSelective(paperRelateSubjectOption);
					}
				}
			}
		}
		record.setTotalScore(totalScore);
		return this.paperMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询考卷编码是否存在
	 *
	 * @param paperNumber
	 * @return
	 * @throws Exception
	 */
	public String selectPaperNumber(String paperNumber) throws Exception {
		return this.paperMapper.selectPaperNumber(paperNumber);
	}

	/**
	 * 查询最大的优先级
	 * 
	 * @return
	 * @throws Exception
	 */
	public Integer selectPrority() throws Exception {
		return this.paperMapper.selectPrority();
	}

	/**
	 * 批量修改状态
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStateByIds(Map<String, Object> map) throws Exception {
		return this.paperMapper.updateStateByIds(map);
	}

	/**
	 * 批量修改状态
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsDelByIds(Map<String, Object> map) throws Exception {
		return this.paperMapper.updateIsDelByIds(map);
	}

	/**
	 * 更新置顶信息
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(Paper record) throws Exception {
		record.setUpdateTime(new Date());
		this.paperMapper.updateIsTop();
		return this.paperMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据优先级查询课程
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Paper selectPaperByPrority(Map<String, Object> map) throws Exception {
		return this.paperMapper.selectPaperByPrority(map);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downPaper
	 * @param upPaper
	 * @return
	 * @throws Exception
	 */
	public int updatePrority(Paper downPaper, Paper upPaper) throws Exception {
		downPaper.setUpdateTime(new Date());
		upPaper.setUpdateTime(new Date());
		this.paperMapper.updateByPrimaryKeySelective(downPaper);
		return this.paperMapper.updateByPrimaryKeySelective(upPaper);
	}

	/**
	 * 任务发布选择试卷分页查询列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TaskSelectPaperVO> taskSelectListByPage(Map<String, Object> params, BasePage<TaskSelectPaperVO> page) throws Exception {
		params.put("page", page);
		List<TaskSelectPaperVO> list = this.paperMapper.taskSelectListByPage(params);
		page.setResults(list);
		return page;
	}

}
